package excel

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	log "github.com/sirupsen/logrus"
	"strconv"
)

func CreateExcelFile(sheetName, filePath string, data [][]string) error {
	xlsx := excelize.NewFile()
	index := xlsx.NewSheet(sheetName)
	row := 1
	for _, v := range data {
		col_index := excelize.TitleToNumber("A")
		for _, vv := range v {
			col := excelize.ToAlphaString(col_index)
			axis := col + strconv.Itoa(row)
			xlsx.SetCellValue(sheetName, axis, vv)
			col_index++
		}
		row++
	}
	xlsx.SetActiveSheet(index)
	SaveAsErr := xlsx.SaveAs(filePath)
	if SaveAsErr != nil {
		log.WithFields(log.Fields{"error": SaveAsErr}).Warn("Excel")
		return SaveAsErr
	}

	return nil
}

func ReadExcelFile(sheetName, filePath string) [][]string {
	excelFile, OpenFileErr := excelize.OpenFile(filePath)
	if OpenFileErr != nil {
		log.WithFields(log.Fields{"error": OpenFileErr}).Warn("Excel")
		return nil
	}

	return excelFile.GetRows(sheetName)
}
